var myUtil = {

    // parentID：父节点
    // children: 子节点数组


    //二维数据转树形数据
    /**
     * @param {*} arr  二维源数据
     * @param {*} topParendID  顶级节点parentID字段值
     * @param {*} keyName  主键字段名称
     */
    list2TreeData:function(arr,topParendID,keyName){
        // 1. 深克隆
        let newArr = JSON.parse(JSON.stringify(arr));
        
        // 2. 定义一个新数组保存新的树形数据
        let treeData = [];

        // 2. 从顶级根节点找子节点
        for(var i in newArr){
            if(newArr[i].parentID==topParendID){
                
                let nowNode = this.getChildren(newArr,newArr[i],keyName);

                treeData.push(nowNode);
            }
        }

        return treeData;

    },
    getChildren:function(arr,item,keyName){
         // 1. 深克隆
         let newArr = JSON.parse(JSON.stringify(arr));

         let cArr = [];
 
         for(var c in newArr){
             if(newArr[c].parentID == item[keyName]){
 
                 let node =  this.getChildren(newArr,newArr[c],keyName);
 
                 cArr.push(node);
             }
         }
 
         item.children = cArr;
 
         return item;
    },
    //树形数据转二维数据
    treeData2List:function(treeData){
        // 1. 深克隆
        let newTreeData = JSON.parse(JSON.stringify(treeData));

        //  2. 结果数组
        let newArr = [];

        for(var i in newTreeData){

            newArr.push(newTreeData[i]);

            if(newTreeData[i].children.length>0){

                this.addChildrenToArr(newArr,newTreeData[i].children);

            }

        }

        for(var c in newArr){
            delete newArr[c].children;
        }
        
        return newArr;
    },
    addChildrenToArr:function(arr,children){
        for(var i in children){
            arr.push(children[i]);
            if(children[i].children.length>0){
                this.addChildrenToArr(arr,children[i].children);
            }
        }
    },
    //获取本机localID
    getLocalID:function(){
        // localStorage 存储，
        // 1. 先从localStorage取出localID
        var localID = window.localStorage.getItem("localID"); // js操作
		if (localID == null) {
			var s = [];
			var hexDigits = "0123456789abcdef";
			for (var i = 0; i < 36; i++) {
				s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
			}
			s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
			s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
			s[8] = s[13] = s[18] = s[23] = "-";

			var uuid = s.join("");
            // 2. 保存到localStorage 中
			window.localStorage.setItem("localID", uuid);
			localID = uuid;
		}
		return localID;

    },
    //将树形数据加工为el-cascader级联选择数据
	listToCascaderTreeData: function(arrs, keyName, nameKey, valueKey, disabledID) {
		let treeArr = JSON.parse(JSON.stringify(arrs));
		treeArr.push({
			parentID: null,
			[nameKey]: "-1",
			[nameKey]: "顶级节点",
			[valueKey]: "-1"
		});
		for (var i in treeArr) {
			treeArr[i].label = treeArr[i][nameKey];
			treeArr[i].value = treeArr[i][valueKey];
			if (disabledID != null || disabledID != undefined || disabledID != "undefined" || disabledID !=
				"") {
				if (treeArr[i][keyName] == disabledID) {
					treeArr[i].disabled = "disabled";
				}
			}
		}
        
		return this.list2TreeData(treeArr,null, keyName);
	},
    getIconCodeList:function(){
        let iconList = ["el-icon-delete-solid",
			"el-icon-delete",
			"el-icon-s-tools",
			"el-icon-setting",
			"el-icon-user-solid",
			"el-icon-user",
			"el-icon-phone",
			"el-icon-phone-outline",
			"el-icon-more",
			"el-icon-more-outline",
			"el-icon-star-on",
			"el-icon-star-off",
			"el-icon-s-goods",
			"el-icon-goods",
			"el-icon-warning",
			"el-icon-warning-outline",
			"el-icon-question",
			"el-icon-info",
			"el-icon-s-help",
			"el-icon-help",
			"el-icon-picture",
			"el-icon-picture-outline",
			"el-icon-picture-outline-round",
			"el-icon-upload",
			"el-icon-upload2",
			"el-icon-download",
			"el-icon-camera-solid",
			"el-icon-camera",
			"el-icon-video-camera-solid",
			"el-icon-video-camera",
			"el-icon-message-solid",
			"el-icon-bell",
			"el-icon-s-cooperation",
			"el-icon-s-order",
			"el-icon-s-platform",
			"el-icon-s-operation",
			"el-icon-s-promotion",
			"el-icon-s-home",
			"el-icon-s-release",
			"el-icon-s-ticket",
			"el-icon-s-management",
			"el-icon-s-open",
			"el-icon-s-shop",
			"el-icon-s-marketing",
			"el-icon-s-flag",
			"el-icon-s-comment",
			"el-icon-s-finance",
			"el-icon-s-claim",
			"el-icon-s-custom",
			"el-icon-s-opportunity",
			"el-icon-s-data",
			"el-icon-s-check",
			"el-icon-s-grid",
			"el-icon-menu",
			"el-icon-share",
			"el-icon-c-scale-to-original",
			"el-icon-date",
			"el-icon-edit",
			"el-icon-edit-outline",
			"el-icon-folder",
			"el-icon-folder-opened",
			"el-icon-tickets",
			"el-icon-document",
			"el-icon-printer",
			"el-icon-paperclip",
			"el-icon-takeaway-box",
			"el-icon-search",
			"el-icon-monitor",
			"el-icon-attract",
			"el-icon-mobile",
			"el-icon-scissors",
			"el-icon-umbrella",
			"el-icon-headset",
			"el-icon-brush",
			"el-icon-mouse",
			"el-icon-coordinate",
			"el-icon-magic-stick",
			"el-icon-reading",
			"el-icon-data-line",
			"el-icon-data-board",
			"el-icon-pie-chart",
			"el-icon-data-analysis",
			"el-icon-collection-tag",
			"el-icon-film",
			"el-icon-suitcase",
			"el-icon-suitcase-1",
			"el-icon-receiving",
			"el-icon-collection",
			"el-icon-files",
			"el-icon-notebook-1",
			"el-icon-notebook-2",
			"el-icon-toilet-paper",
			"el-icon-office-building",
			"el-icon-school",
			"el-icon-table-lamp",
			"el-icon-house",
			"el-icon-no-smoking",
			"el-icon-smoking",
			"el-icon-shopping-cart-full",
			"el-icon-shopping-cart-1",
			"el-icon-shopping-cart-2",
			"el-icon-shopping-bag-1",
			"el-icon-shopping-bag-2",
			"el-icon-sold-out",
			"el-icon-sell",
			"el-icon-present",
			"el-icon-box",
			"el-icon-bank-card",
			"el-icon-money",
			"el-icon-coin",
			"el-icon-wallet",
			"el-icon-discount",
			"el-icon-price-tag",
			"el-icon-news",
			"el-icon-guide",
			"el-icon-male",
			"el-icon-female",
			"el-icon-thumb",
			"el-icon-cpu",
			"el-icon-link",
			"el-icon-connection",
			"el-icon-open",
			"el-icon-turn-off",
			"el-icon-set-up",
			"el-icon-chat-round",
			"el-icon-chat-line-round",
			"el-icon-chat-square",
			"el-icon-chat-dot-round",
			"el-icon-chat-dot-square",
			"el-icon-chat-line-square",
			"el-icon-message",
			"el-icon-postcard",
			"el-icon-position",
			"el-icon-turn-off-microphone",
			"el-icon-microphone",
			"el-icon-close-notification",
			"el-icon-bangzhu",
			"el-icon-time",
			"el-icon-odometer",
			"el-icon-crop",
			"el-icon-aim",
			"el-icon-switch-button",
			"el-icon-full-screen",
			"el-icon-copy-document",
			"el-icon-mic",
			"el-icon-stopwatch"
		];

		return iconList;
    },
    // 字典表翻译功能
    getParamInfoNameByCode: function(arrs, value) {
		let name = "";
		for (var i in arrs) {
			if (arrs[i].dataValue == value) {
				name = arrs[i].dataName;
				break;
			}
		}
		return name;
	}

};

export default myUtil;